| Conditions | 10 |
| Paths | 4 |
| Total Lines | 17 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Complex classes like elementContains.js ➔ elementContains often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | export function elementContains(el, ...classes) { |
||
| 2 | |||
| 3 | if (!el || !classes || !classes.length) { |
||
| 4 | throw Error('Function requires a dom node and a classname'); |
||
| 5 | } |
||
| 6 | |||
| 7 | while (el && el !== document.body) { |
||
| 8 | if (el && el.classList) { |
||
| 9 | for (let i = 0; i < classes.length; i++) { |
||
| 10 | if (el.classList.contains(classes[i])) { |
||
| 11 | return true; |
||
| 12 | } |
||
| 13 | } |
||
| 14 | } |
||
| 15 | el = el.parentNode; |
||
| 16 | } |
||
|
|
|||
| 17 | } |